
package repositorios;

import interfaces.IRepositorioUsuario;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import modelos.Empresa;
import modelos.Usuario;
import util.GerenteConexao;

public class RepositorioUsuario implements IRepositorioUsuario{

	public void adicionarUsuario(Usuario u){
		String sql = "insert into usuario( id, nome, cpf, rg, sexo, data_nascimento, endereco, cidade, estado, email, senha, perfil) values(?,?,?,?,?,?,?,?,?,?,?,?)";

		try {
			PreparedStatement ps = GerenteConexao.getConexao().prepareStatement(sql);
			montarOperacao(ps, u);

			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void removerUsuario(Usuario u){

		String sql = "delete from usuario where id = ?";
		try{
			PreparedStatement ps = GerenteConexao.getConexao().prepareStatement(sql);
			ps.setLong(1, u.getId());
			ps.executeUpdate();

		} catch(Exception e){
			e.printStackTrace();
		}
	}

	public List<Usuario> listarUsuario(){
		String sql = "Select * from usuario";
		List<Usuario> retorno = null;
		try {
			Statement st = GerenteConexao.getConexao().createStatement();
			ResultSet rs = st.executeQuery(sql);
			retorno = new ArrayList<Usuario>();

			while(rs.next()){
				Usuario u = montarObjeto(rs);
				retorno.add(u);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retorno;
	}

	public void editarUsuario(Usuario u){
		String sql = "update usuario set id=?, nome =?, cpf =?, rg =?, sexo =?," +
				" data_nascimento =?, endereco=?, cidade =?, estado =?, email =?, senha =?" +
				", perfil =? where id ="+u.getId();

		try {
			PreparedStatement ps = GerenteConexao.getConexao().prepareStatement(sql);
			montarOperacao(ps, u);
			
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Usuario buscarUsuarioPorId(Long id){
		String sql = "select * from usuario where id = "+ id;
		Usuario u = null;

		try{
			Statement st = GerenteConexao.getConexao().createStatement();
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()){
				u = montarObjeto(rs);
			}
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		return u;
	}
	
	public Usuario logar(String email, String senha){
		String sql = "select * from usuario where email = '"+ email + "' AND senha = '" + senha + "'";
		Usuario u = null;

		try{
			Statement st = GerenteConexao.getConexao().createStatement();
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()){
				u = montarObjeto(rs);
			}
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		return u;
	}
	
	public List<Usuario> buscarLike(String campo, String valor){
		String sql = "Select * FROM usuario WHERE " + campo + " like '%" + valor + "%'";

		List<Usuario> retorno = null;
		try {
			Statement st = GerenteConexao.getConexao().createStatement();
			ResultSet rs = st.executeQuery(sql);
			retorno = new ArrayList<Usuario>();

			while(rs.next()){
				Usuario u = montarObjeto(rs);
				retorno.add(u);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retorno;
	}
	
	private Usuario montarObjeto(ResultSet rs){
		Usuario u = new Usuario();
		try {
			u.setId(rs.getInt("id"));
			u.setNome(rs.getString("nome"));
			u.setCpf(rs.getString("cpf"));
			u.setRg(rs.getString("rg"));
			u.setSexo(rs.getString("sexo"));
			u.setDataNascimento(rs.getString("data_nascimento"));
			u.setEndereco(rs.getString("endereco"));
			u.setCidade(rs.getString("cidade"));
			u.setEstado(rs.getString("estado"));
			u.setEmail(rs.getString("email"));
			u.setSenha(rs.getString("senha"));
			u.setPerfil(rs.getString("perfil"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return u;
	}
	
	private void montarOperacao(PreparedStatement ps, Usuario u){

		try {
			ps.setLong(1, u.getId());
			ps.setString(2, u.getNome());
			ps.setString(3, u.getCpf());
			ps.setString(4, u.getRg());
			ps.setString(5, u.getSexo());
			ps.setString(6, u.getDataNascimento());
			ps.setString(7, u.getEndereco());
			ps.setString(8, u.getCidade());
			ps.setString(9, u.getEstado());
			ps.setString(10, u.getEmail());
			ps.setString(11, u.getSenha());
			ps.setString(12, u.getPerfil());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
